package com.zombie.input;

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.zombie.Constants;
import com.zombie.ZombieGame;
import com.zombie.logic.core.ZombieWorld;
import com.zombie.state.GameState;
import com.zombie.util.state.transition.FadeInTransition;
import com.zombie.util.state.transition.FadeOutTransition;

public class Input implements InputProcessor {

	public static int mouseX;
	public static int mouseY;
	private static boolean[] pressed = new boolean[256];
	private static boolean[] mouseButtons = new boolean[3];
	static List<InputProcessor> list = new ArrayList<InputProcessor>();
	public static boolean isPressed(int key){
		return pressed[key];
	}
	
	public static boolean isMousePressed(int key){
		return mouseButtons[key];
	}	
	@Override
	public boolean keyDown(int keycode) {
		pressed[keycode] = true;
		for(InputProcessor input : list)
			if (input.keyDown(keycode))
				return true;
//		if (Keys.SPACE == keycode){
//			ZombieWorld.init(0);
//			ZombieGame.getInstance().enterState(Constants.STATE_GAME, new FadeOutTransition(Color.RED), new FadeInTransition(Color.RED));
//		}
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		pressed[keycode] = false;
		for(InputProcessor input : list)
			if (input.keyUp(keycode))
				return true;
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		for(InputProcessor input : list)
			if (input.keyTyped(character))
				return true;
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		mouseX = screenX;
		mouseY = screenY;
		mouseButtons[button] = true;
		for(InputProcessor input : list)
			if (input.touchDown(screenX, screenY, pointer, button))
				return true;
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		mouseX = screenX;
		mouseY = screenY;
		mouseButtons[button] = false; 
		if (button == Buttons.MIDDLE)
			GameState.getInstance().zoom = 1;

		for(InputProcessor input : list)
			if (input.touchUp(screenX, screenY, pointer, button))
				return true;
		return false;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		mouseX = screenX;
		mouseY = screenY;
		for(InputProcessor input : list)
			if (input.touchDragged(screenX, screenY, pointer))
				return true;
		return false;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		mouseX = screenX;
		mouseY = screenY;
		for(InputProcessor input : list)
			if (input.mouseMoved(screenX, screenY))
				return true;
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		for(InputProcessor input : list)
			if (input.scrolled(amount))
				return true;	
		GameState.getInstance().zoom+= amount/20f;
		return false;
	}

	public static void addInputProcessor(InputProcessor input) {
		list.add(input);
	}

	public static void removeInputProcessor(InputProcessor input) {
		list.remove(input);
	}
	
}
